package org.budo.mybatis.dao.entity.configer.impl;

import java.lang.reflect.Field;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import org.budo.mybatis.dao.entity.configer.MyBatisDaoEntityConfiger;
import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author limingwei
 */
public class AnnotationEntityConfiger implements MyBatisDaoEntityConfiger {
    private static final Logger log = Slf4j.getLogger();

    public String getTableName(Class<?> entityType) {
        Table table = ReflectUtil.getAnnotation(entityType, Table.class);
        if (null == table) {
            return entityType.getSimpleName();
        }
        return table.name();
    }

    public String getIdColumn(Class<?> entityType) {
        List<Field> fields = ReflectUtil.getAllFields(entityType);
        for (Field field : fields) {
            Id id = field.getAnnotation(Id.class);
            if (null != id) {
                return field.getName();
            }
        }

        for (Field field : fields) {
            if ("id".equals(field.getName())) {
                return "id";
            }
        }

        log.error(entityType + " has no idColumn");
        return null;
    }

    public String getIdFieldName(Class<?> entityType) {
        List<Field> fields = ReflectUtil.getAllFields(entityType);
        for (Field field : fields) {
            Id id = field.getAnnotation(Id.class);
            if (null != id) {
                return field.getName();
            }
        }

        for (Field field : fields) {
            if ("id".equals(field.getName())) {
                return "id";
            }
        }

        log.error(entityType + " has no idField");
        return null;
    }

    public Class<?> getIdFieldType(Class<?> entityType) {
        List<Field> fields = ReflectUtil.getAllFields(entityType);
        for (Field field : fields) {
            Id id = field.getAnnotation(Id.class);
            if (null != id) {
                return field.getType();
            }
        }

        for (Field field : fields) {
            if ("id".equals(field.getName())) {
                return field.getType();
            }
        }

        log.error(entityType + " has no idField");
        return null;
    }

    public String getColumnName(Field field) {
        Column column = field.getAnnotation(Column.class);
        if (null == column || null == column.name() || column.name().isEmpty()) {
            return field.getName();
        } else {
            return column.name();
        }
    }

    /**
     * 表外键方式必须加 @JoinColumn，不加 @JoinColumn 的 @ManyToOne 视为查询视图
     */
    public String getJoinColumnName(Field field) {
        JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);
        if (null != joinColumn) {
            return joinColumn.name();
        }

        return null;
    }
}